package com.mvcplus.system.controller;

import com.mvcplus.common.controller.BaseController;
import com.mvcplus.common.utils.Datatable;
import com.mvcplus.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by BINGO on 2016/10/28.
 * 角色管理
 */
@Controller
@Scope(value = "prototype")
@RequestMapping("sysRole")
public class SysRoleController extends BaseController{

    private final ISysRoleService roleService;

    @Autowired
    public SysRoleController(ISysRoleService roleService) {
        this.roleService = roleService;
    }

    @RequestMapping("/")
    public ModelAndView index(ModelAndView modelAndView){
        modelAndView.setViewName("system/role");
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/datatable")
    public Object datatable(HttpServletRequest request){
        Integer draw = 1;
        Map<String, String[]> params = request.getParameterMap();
        for (String attr : params.keySet()) {
            String[] val = params.get(attr);
            if (attr.equals("draw"))
                draw = Integer.parseInt(val[0]);
        }
        Datatable dt = new Datatable(draw + 1, roleService.selectList(null));
        return dt;
    }

}
